package com.haizhi.crm.recommend.service.imp;

import com.haizhi.crm.common.constant.CrmSchema;
import com.haizhi.crm.common.json.JSONUtils;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.util.Getter;
import com.haizhi.crm.recommend.constant.RecType;
import com.haizhi.crm.recommend.model.RecQo;
import com.haizhi.crm.recommend.model.RecVo;
import com.haizhi.crm.recommend.service.RecService;
import com.haizhi.crm.search.builder.SearchQoBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yangyijun on 2018/7/11.
 */
@Service
public class CompanyAreaNews extends BaseRecService implements RecService {
    public static final GLog LOG = new GLog(CompanyAreaNews.class);

    @Override
    public String getName() {
        return RecType.Company.DETAIL_AREA_NEWS;
    }

    @Override
    public RecVo doRecommend(RecQo recQo) {
        try {
            //查询公司的省份
            String province = getProvince(recQo);

            //查询行外事件
            List<Map<String, Object>> eventsMap = getOuterEvents(recQo, province);
            return (RecVo) new RecVo().setPayload(eventsMap);
        } catch (Exception e) {
            LOG.error("recommend company area news error!", e);
            return (RecVo) new RecVo().setPayload(new ArrayList<>());
        }
    }

    private List<Map<String, Object>> getOuterEvents(RecQo recQo, String province) {
        SearchQoBuilder eventBuilder = new SearchQoBuilder();
        eventBuilder.setGraph(recQo.getGraph());
        eventBuilder.addSchemas(CrmSchema.EVENT_OUTTER);
        eventBuilder.setPageNo(1);
        eventBuilder.setPageSize(5);
        eventBuilder.addTermFilter("address", province);
        eventBuilder.addSort("event_time", "desc" );
        Object events = searchService.search(eventBuilder.get()).getSearchResp().getData();
        return JSONUtils.toListMap(events);
    }

    private String getProvince(RecQo recQo) {
        String objectKey = recQo.getVertexId();
        SearchQoBuilder searchQoBuilder = new SearchQoBuilder();
        searchQoBuilder.setGraph(recQo.getGraph());
        searchQoBuilder.addSchemas(CrmSchema.COMPANY);
        searchQoBuilder.addTermFilter("object_key", objectKey);
        Object data = searchService.search(searchQoBuilder.get()).getSearchResp().getData();
        Map<String, Object> company = JSONUtils.toMap(data);
        return Getter.get("reg_province", company);
    }


//    private List<Map<String, Object>> getOuterEvents(RecQo recQo, String province) {
//        SearchQoBuilder eventBuilder = new SearchQoBuilder();
//        eventBuilder.setGraph(recQo.getGraph());
//        eventBuilder.addSchemas(CrmSchema.EVENT_OUTTER);
//        eventBuilder.setPageNo(1);
//        eventBuilder.setPageSize(5);
//        Map<String, Object> keysMap = new HashMap<>();
//        keysMap.put(EsOperator.KEY_FIELD, "company_keys");
//        keysMap.put(EsOperator.KEY_TYPE, EsOperator.MATCH);
//        keysMap.put(EsOperator.KEY_VALUES, companyKeys.toArray());
//        eventBuilder.get().addFilter(keysMap);
//        eventBuilder.addSort("event_time", "desc");
//        Object events = searchService.search(eventBuilder.get()).getSearchResp().getData();
//        return JSONUtils.toListMap(events);
//    }
//
//    private List<String> getCompanyKeys(RecQo recQo, String province) {
//        SearchQoBuilder builder = new SearchQoBuilder();
//        builder.setGraph(recQo.getGraph());
//        builder.addSchemas(CrmSchema.COMPANY);
//        builder.addTermFilter("reg_province", province);
//        Object datas = searchService.search(builder.get()).getSearchResp().getData();
//        List<String> companyKeys = new ArrayList<>();
//        List<Map<String, Object>> companys = JSONUtils.toListMap(datas);
//        companys.forEach(c -> companyKeys.add(Getter.get("object_key", c)));
//        companyKeys.remove(recQo.getVertexId());
//        return companyKeys;
//    }
}
